package main

import (
	"fmt"
	"github.com/xuri/excelize/v2"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

const (
	DSN             = "root:changyz...@(42.192.221.26:3306)/gin_class?charset=utf8mb4&parseTime=True&loc=Local"
	CreateBatchSize = 1000
)

func getInitDb() *gorm.DB {
	db, err := gorm.Open(mysql.Open(DSN), &gorm.Config{})
	if err != nil {
		fmt.Println(err)
		recover()
	}
	return db
}

func saveBatch(stores []*Store) {
	db := getInitDb()
	db.Session(&gorm.Session{CreateBatchSize: CreateBatchSize})
	db.AutoMigrate(&Store{})

	tx := db.CreateInBatches(stores, CreateBatchSize)
	if tx.Error != nil {
		tx.Rollback()
		fmt.Printf("批量插入失败 %s ln", tx.Error.Error())
		return
	}
}

func main() {
	f, err := excelize.OpenFile("D:\\export\\t_store.xlsx")
	if err != nil {
		fmt.Printf("读取数据失败：%s \n", err.Error())
		return
	}

	defer func() {
		if err := f.Close(); err != nil {
			fmt.Println(err.Error())
		}
	}()

	rows, err := f.GetRows("t_store")
	if err != nil {
		fmt.Printf("读取数据失败：%s \n", err.Error())
		return
	}

	//遍历行的每一个数据
	var stores = make([]*Store, len(rows)-1)
	for key, row := range rows[1:] {
		store := Store{
			Uuid:               row[0],
			Name:               row[1],
			Phone:              row[2],
			BusinessStartHours: row[3],
			BusinessEndHours:   row[4],
			StoreContact:       row[5],
			ContactPhone:       row[6],
			ProvinceCode:       row[7],
			Province:           row[8],
			CityCode:           row[9],
			City:               row[10],
			Address:            row[11],
			Longitude:          row[12],
			Latitude:           row[13],
			CreateTime:         row[14],
			UpdateTime:         row[15],
			State:              row[16],
			StoreParentUuid:    row[17],
			StoreCode:          row[18],
			BusinessScope:      row[19],
			FileUrl:            row[20],
			Area:               row[21],
			AreaCode:           row[22],
			DisableFlag:        row[23],
			StoreGroupUuid:     row[24],
			JnBusno:            row[25],
			InvitationCode:     row[26],
			FlowStatus:         row[27],
			SourceType:         row[28],
			Plaque:             row[29],
			Checkout:           row[30],
			InStore:            row[31],
			MerTrace:           row[32],
			StreamNo:           row[33],
			TermCode:           row[34],
			TermMercode:        row[35],
			TermTermcode:       row[36],
			BusinessNo:         row[37],
			AuditStatus:        row[38],
		}
		stores[key] = &store
	}

	saveBatch(stores)

	//for _, s := range stores {
	//	fmt.Printf("Store结构体内容 %v \n", s)
	//}
}

func (Store) TableName() string {
	return "t_store"
}

type Store struct {
	Uuid               string `gorm:"default:;"`
	Name               string
	Phone              string
	BusinessStartHours string
	BusinessEndHours   string
	StoreContact       string
	ContactPhone       string
	ProvinceCode       string
	Province           string
	CityCode           string
	City               string
	Address            string
	Longitude          string
	Latitude           string
	CreateTime         string
	UpdateTime         string
	State              string
	StoreParentUuid    string
	StoreCode          string
	BusinessScope      string
	FileUrl            string
	Area               string
	AreaCode           string
	DisableFlag        string
	StoreGroupUuid     string
	JnBusno            string
	InvitationCode     string
	FlowStatus         string
	SourceType         string
	Plaque             string
	Checkout           string
	InStore            string
	MerTrace           string
	StreamNo           string
	TermCode           string
	TermMercode        string
	TermTermcode       string
	BusinessNo         string
	AuditStatus        string
}
